package com.yinp.proapp.module.wanandroid.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.yinp.proapp.adapter.ComViewHolder;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.activity.PresenterWanAndroidActivity;
import com.yinp.proapp.databinding.ActivityWanCollectionBinding;
import com.yinp.proapp.databinding.ItemWanCollectListBinding;
import com.yinp.proapp.module.wanandroid.bean.CollectionListBean;
import com.yinp.proapp.module.wanandroid.dialog.DialogShow;
import com.yinp.proapp.module.wanandroid.interfaces.CollectListener;
import com.yinp.proapp.module.wanandroid.web.retrofit.WanData2;
import com.yinp.proapp.module.wanandroid.web.retrofit.WanObserver2;
import com.yinp.proapp.utils.AppUtils;
import com.yinp.proapp.utils.JumpWebUtils;
import com.yinp.proapp.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的收藏
 */
public class WanCollectionActivity extends PresenterWanAndroidActivity<ActivityWanCollectionBinding> {
    private List<CollectionListBean.DatasBean> dataList = new ArrayList<>();
    private CommonAdapter<CollectionListBean.DatasBean> commonAdapter;

    private int page = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        bd.header.headerCenterTitle.setText("我的收藏");
        initClick(bd.header.headerBackImg);
        initRecycler();
        refresh();
        getCollectInfo(true);
    }

    private void initRecycler() {
        commonAdapter = new CommonAdapter<CollectionListBean.DatasBean>(mContext,dataList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                ItemWanCollectListBinding binding = ItemWanCollectListBinding.inflate(mInflater, parent, false);
                SingleViewHolder holder = new SingleViewHolder(binding);
                binding.ivCollect.setOnClickListener(v -> {
                    int position = holder.getAbsoluteAdapterPosition();
                    if (AppUtils.isLogin(mContext)) {
                        if (!dataList.get(position).isCollect()) {
                            presenter.addCollect(dataList.get(position).getOriginId(), new CollectListener() {
                                @Override
                                public void Success() {
                                    dataList.get(position).setCollect(true);
                                    commonAdapter.notifyItemChanged(position, "");
                                }

                                @Override
                                public void Failed() {

                                }
                            });
                        } else {
                            presenter.cancelCollect(dataList.get(holder.getAbsoluteAdapterPosition()).getOriginId(), new CollectListener() {
                                @Override
                                public void Success() {
                                    dataList.get(position).setCollect(false);
                                    commonAdapter.notifyItemChanged(position, "");
                                }

                                @Override
                                public void Failed() {

                                }
                            });
                        }
                    } else {
                        DialogShow.setLoginDialog(mContext, presenter, false, WanMeActivity.class, getSupportFragmentManager());
                    }
                });
                return holder;
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, CollectionListBean.DatasBean item) {
                ItemWanCollectListBinding binding = (ItemWanCollectListBinding) holder.getBinding();
                binding.tvTitle.setText(AppUtils.getValue(item.getTitle()));

//                HtmlImageGetter htmlImageGetter = new HtmlImageGetter(binding.htvContent);
//                htmlImageGetter.enableCompressImage(true, 100);
//                binding.htvContent.setHtml(item.getDesc(), htmlImageGetter);

                binding.tvAuthor.setText(AppUtils.getValue(item.getAuthor()));
                binding.tvTime.setText(item.getNiceDate());
                binding.tvChapterName.setText(item.getChapterName());
                if (item.isCollect()) {
                    binding.ivCollect.setSelected(true);
                } else {
                    binding.ivCollect.setSelected(false);
                }
            }
        };
        commonAdapter.setOnItemClickListener(new ComViewHolder.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                CollectionListBean.DatasBean item = dataList.get(position);
                if (!TextUtils.isEmpty(item.getLink())) {
                    JumpWebUtils.startWebView(mContext, item.getTitle(), item.getLink());
                } else {
                    showToast("缺少链接地址");
                }
            }
        });
        bd.baseRecycle.setLayoutManager(new LinearLayoutManager(mContext));
        bd.baseRecycle.setHasFixedSize(true);
        bd.baseRecycle.setAdapter(commonAdapter);
    }

    static class ViewHolder extends ComViewHolder {
        ItemWanCollectListBinding binding;

        public ViewHolder(ItemWanCollectListBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {
            finish();
        }
    }

    private void refresh() {
        //为下来刷新添加事件
        bd.baseRefresh.setOnRefreshListener(refreshlayout -> {
            page = 0;
            getCollectInfo(false);
        });
        //为上拉加载添加事件
        bd.baseRefresh.setOnLoadMoreListener(refreshLayout -> {
            page++;
            getCollectInfo(false);
        });
    }

    /**
     * 获取收藏列表
     */
    private void getCollectInfo(boolean isLoad) {
        if (isLoad) {
            showLoading("加载中...");
        }
        presenter.getCollectedList(page, new WanObserver2<WanData2<CollectionListBean>>() {
            @Override
            public void onSuccess(WanData2<CollectionListBean> o) {
                hideLoading();
                if (page > 0)
                    bd.baseRefresh.finishLoadMore(true);
                else
                    bd.baseRefresh.finishRefresh(true);
                if (o.getData() != null) {
                    if (o.getData().getDatas() != null && o.getData().getDatas().size() > 0) {
                        int start = dataList.size();
                        dataList.addAll(o.getData().getDatas());
                        int end = dataList.size();
                        commonAdapter.notifyItemRangeChanged(start - 1, end);
                        bd.baseRefresh.setVisibility(View.VISIBLE);
                        bd.bottom.noLl.setVisibility(View.GONE);
                    }
                } else {
                    bd.baseRefresh.setVisibility(View.GONE);
                    bd.bottom.noLl.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onCodeFail(String msg, int code) {
                if (page > 0)
                    bd.baseRefresh.finishLoadMore(false);
                else
                    bd.baseRefresh.finishRefresh(false);
                hideLoading();
                showToast(msg);
            }
        });
    }
}